<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑用户</title>
    <style>
        form {
            width: 300px;
            margin: 0 auto;
        }
        input[type=text], input[type=password], input[type=date] {
            width: 100%;
            padding: 8px;
            margin: 5px 0;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type=submit] {
            width: 100%;
            background-color: #4CAF50;
            color: white;
            padding: 10px;
            margin: 8px 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type=submit]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<h1>编辑用户</h1>

<%
    // 数据库中 userNo 可能是字符串类型，若为数值类型可继续用 int
    String userId = request.getParameter("id");
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
        String url = "jdbc:mysql://localhost:3306/user";
        String username = "root";
        String password = "12345";

        conn = DriverManager.getConnection(url, username, password);
        // 使用数据库实际字段 userNo 查询
        String sql = "SELECT * FROM user2 WHERE userNo = ?";
        stmt = conn.prepareStatement(sql);
        stmt.setString(1, userId); // 匹配字符串类型
        rs = stmt.executeQuery();

        if (rs.next()) {
%>

<!-- 修复：提交到正确的 Servlet（manageuserServlet） -->
<form action="manageuserServlet?action=update" method="post">
    <!-- 隐藏域存储 userNo（数据库主键） -->
    <input type="hidden" name="userNo" value="<%= rs.getString("userNo") %>">

    <!-- 匹配数据库字段 -->
    <label>用户名:</label>
    <input type="text" name="userName" value="<%= rs.getString("userName") %>" required><br>

    <label>账号:</label>
    <input type="text" name="userNo" value="<%= rs.getString("userNo") %>" required><br>

    <label>密码: (不修改则留空)</label>
    <input type="password" name="userPwd"><br>

    <label>生日:</label>
    <input type="date" name="birthday" value="<%= rs.getString("birthday") %>"><br>

    <input type="submit" value="保存">
</form>

<%
        } else {
            out.println("未找到该用户");
        }
    } catch (SQLException e) {
        e.printStackTrace();
        out.println("查询用户信息时出现错误：" + e.getMessage());
    } finally {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>

<a href="manageuserServlet?action=list">返回用户列表</a>
</body>
</html>